import 'dart:convert';

import 'package:bruno/bruno.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:sph_driver/http/NoLoadingGetApi.dart';

import '../AppConfig/Config.dart';
import '../AppConfig/LocalStorage.dart';
import '../FindGoods/GoodsList.dart';
import '../Main/PersonCenter.dart';
import '../MatchingGoods/MatchingView.dart';
import '../Order/OrderList.dart';
import '../RoadLine/RoadLineView.dart';
import '../http/api.dart';
import '../l10n/messages.dart';
class TabbarView extends StatefulWidget {
  const TabbarView({super.key, required this.pateIndex});
  final int pateIndex;
  @override
  State<TabbarView> createState() => _TabbarViewState();
}

class _TabbarViewState extends State<TabbarView> {
  List pages = [GoodsList(),RoadLineView(), OrderList(),PersonCenter()];
  int currentPage =0;
@override
  void initState() {
    // TODO: implement initState
    super.initState();
    // getUserMessageAction();
    if (widget.pateIndex!=null) currentPage=widget.pateIndex;
    getAPPVersion();
  }
  void getUserMessageAction() async {
    NoLoadinggetApiManager.getData(
      context, // 上下文对象，用于显示提示信息等
      'api/auth/driver/user/getLoginUser', // 您的API端点
      {}, // 查询参数，如果没有可以传递null
          (responseData)  {
      },
    );
  }
  void getAPPVersion(){
    NoLoadinggetApiManager.getData(
      context,
      "api/system/dev/dict/list",
      {'category':'BIZ','parentId':'1808424071131934722'},
          (responseData)  {
        setState(() {
          if(responseData['data'][1]['extJson']!="v1.0.0"){
            showDialog(
              barrierDismissible: false,
              context: context,
              builder: (BuildContext context) {
                return Align(
                  alignment: Alignment.center,
                  child: BrnDialog(
                    titleText: "温馨提示",
                    messageText: "速配货有新的版本上线，请前往更新。",
                    actionsWidget: [
                      TextButton(
                        onPressed: () {
                          Navigator.pop(context);
                        },
                        child: Text(
                          "下次再说",
                          style: TextStyle(color: MyColors.textColor, fontSize: 14.sp),
                        ),
                      ),
                      TextButton(
                        onPressed: () {
                          Navigator.pop(context);
                        },
                        child: Text(
                          "现在就去",
                          style: TextStyle(color: MyColors.themeColor, fontSize: 14.sp),
                        ),
                      ),
                    ],
                  ),
                );
              },
            );
          }
        });

      },
    );
  }
  DateTime? lastPressed;
  @override
  Widget build(BuildContext context) {
    var messages = AppLocalizations.of(context)!;
    return  WillPopScope(
        onWillPop: () async {
          print("再按一次退出");
          // 获取当前时间
          final now = DateTime.now();
          // 如果 lastPressed 为空或者间隔时间超过 2 秒，则更新 lastPressed 并提示用户
          if (lastPressed == null || now.difference(lastPressed!) > Duration(seconds: 2)) {
            lastPressed = now;
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(
                content: Text('再按一次退出'),
                duration: Duration(seconds: 2),
              ),
            );
            return false; // 不退出
          }
          return true; // 退出应用
    },
    child:
      Scaffold(
      bottomNavigationBar: BottomNavigationBar(
          backgroundColor: Colors.white,
          items: [

            BottomNavigationBarItem(
              icon:currentPage==0? Image.asset('lib/Images/Tabbar/找货黄icon.png',height: 20.h,width: 20.w,):Image.asset('lib/Images/Tabbar/找货icon.png',height: 20.h,width: 20.w,),
              label: messages['找货'],
            ),
            BottomNavigationBarItem(
                    icon:currentPage==1? Image.asset('lib/Images/Tabbar/常跑路线Click.png',height: 20.h,width: 20.w,):Image.asset('lib/Images/Tabbar/常跑路线Tab.png',height: 20.h,width: 20.w,),
                  label: '常跑路线',
                ),

            BottomNavigationBarItem(
              icon:Stack(
                children: [
                  currentPage==2? Image.asset('lib/Images/Tabbar/运单Click.png',height: 15.h,width: 20.w,):Image.asset('lib/Images/Tabbar/运单Tab.png',height: 15.h,width: 20.w,),
                  Positioned(
                    top: 0,
                    right: 0,
                    child:  Badge(backgroundColor: Colors.red,),)
                ],
              ),
              label:'运单',
            ),
              BottomNavigationBarItem(
                icon:currentPage==3? Image.asset('lib/Images/Tabbar/我的Click.png',height: 20.h,width: 20.w,):Image.asset('lib/Images/Tabbar/我的Tab.png',height: 20.h,width: 20.w,),
              label: '我的',
              ),],

          type: BottomNavigationBarType.fixed, currentIndex: currentPage,onTap: (index){
        setState(() {
          currentPage=index;

        });

      },selectedItemColor: MyColors.themeColor),
      body: pages[currentPage],
      ),
    );
  }
}
